<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/10/17  16:05
 */
namespace app\admin\controller;
class Adviser extends Common
{
    private $table_admin = '';

    /*
	*	构造函数
	*/
    function __construct()
    {
        parent::__construct();
        $this->table_admin = db('adviser');
    }

    //列表
    public function lists()
    {
        $pageParam = ['query' =>[]];
        $where = [];
        $keyword = input("keyword");
        if($keyword != ''){
            $where['advisername']=array("like",'%'.input('keyword').'%');
            $pageParam['query']['keyword'] = $keyword;
        }
        $this->assign('keyword', $keyword);
        $adviser = $this->table_admin->order('id desc')->paginate(15, false, $pageParam);
        $this->assign('adviser', $adviser);
        $this->assign('page', $adviser->render());
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch('adviser/edit');
    }

    //编辑
    public function edit()
    {
        if(request()->isAjax()){
            $id = input('id');
            $advisername = input('advisername');
            $level = input('level');
            $item = input('item');
            $introduction = input('introduction');
            $is_zheng = input('is_zheng');
            $is_nuo = input('is_nuo');
            $sex = input('sex');
            $status = input('status');
            $sort = input('sort');
            $data['advisername'] = $advisername;
            $data['level'] = $level;
            $data['item'] = $item;
            $data['introduction'] = $introduction;
            $data['is_zheng'] = $is_zheng;
            $data['is_nuo'] = $is_nuo;
            $data['sex'] = $sex;
            $data['status'] = $status;
            $data['sort'] = $sort;
            $file = request()->file('image');
            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public/static/uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['head_img'] =  $info->getSaveName();
                    if(!empty($id)){
                        $oldimg = db('adviser')->where(['id'=>$id])->value('head_img');
                        $oldimgurl = ROOT_PATH . 'public/static/uploads/'.$oldimg;
                        if(file_exists($oldimgurl) && !empty($oldimg)){
                            unlink($oldimgurl);
                        }
                    }
                }else{
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }
            if(empty($id)){
                //添加
                $data['creatime'] = time();
                $bool = $this->table_admin->insert($data);
            }else{
                //修改
                $bool = $this->table_admin->where(['id'=>$id])->update($data);
            }

            if(empty($bool))$this->error('操作失败！');
            $this->success('操作成功！', url('Adviser/lists'));
        }

        $id = input('id');
        if(!empty($id)){
            $da = $this->table_admin->where(['id'=>$id])->find();
            $this->assign('da', $da);
        }
        return $this->fetch();
    }

    //删除
    public function del()
    {
        $id = input('id');
        if(!$id)$this->error('参数有误');
        $data = $this->table_admin->where(['id'=>$id])->find();
        if(!$data)$this->error('数据不存在');
        $img = ROOT_PATH . 'public/static/uploads/'.$data['head_img'];
        if($data['head_img'] || is_file($img)){
            unlink($img);
        }
        $res = $this->table_admin->where(['id'=>$id])->delete();
        if($res)$this->success('操作成功！');
        $this->error('操作失败！');
    }
}